package com.neutech.solar.client;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.entity.*;

import java.awt.*;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

//new对象运行的类
public class SolarSystemClient extends AbstractFrame {
    Background bg = new Background("sky01.jpg");
    Sun sun = new Sun();
    Planet Earth = new Planet(sun,"earth", 1, 0.167, 365,26,26);//地球
    Planet Mars = new Planet(sun,"mars",1.52,0.0934,687,14,14);//火星
    Planet Mercury = new Planet(sun,"mercury",0.387,0.2056,88,10,10);//水星
    Planet Venus = new Planet(sun,"venus",0.72,0.0068,225,25,25,false,true);//金星
    Planet Jupiter = new Planet(sun,"jupiter",5.2,0.0489,4330,293,293);//木星
    Planet Saturn = new Planet(sun,"saturn",9.54,0.0557,10832,247,247);//土星
    Planet Uranus = new Planet(sun,"uranus",19.218,0.0444,60328,105,105);//天王星
    Planet Neptune = new Planet(sun,"neptune",30.06,0.0112,90717,102,102);//海王星
    Planet Pluto = new Planet(sun,"pluto",39.68,0.249,90717,5,5);//冥王星
    Planet Halley = new Planet(sun,"Ceres",17.8,0.967,27485);//哈雷彗星
    Planet moon = new Planet(Earth,"Ceres",0.3,0.0,365/12);
    Shower shower = new Shower("shower", 1800, 0);
    List<Planet> planets = new ArrayList<>();
    Random random = new Random();
    {
        for (int i = 0; i < 10000; i++) {
            double AU = random.nextDouble() * 2 + 2.0;
            double e = random.nextDouble();
            int T = random.nextInt(3300) + 700;
            Planet smallPlanet = new Planet(sun,"smallPlanet",AU,0.0,T,true);
            planets.add(smallPlanet);
        }
    }
    //这里会报异常，要用CopyOnWriteArrayList解决
    List<Shower> showers = new CopyOnWriteArrayList<>();
    {
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                int shower_x = random.nextInt(1000) + 800;
                int shower_y = random.nextInt(200);
                Shower shower1 = new Shower("shower",shower_x,shower_y);
                showers.add(shower1);
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }



    public SolarSystemClient() throws HeadlessException {
        super.init("太阳系八大行星运行轨迹图");
    }

    @Override
    public void paint(Graphics g) {
        bg.draw(g);

        for (Shower shower : showers){
            shower.draw(g);
        }
        sun.draw(g);
        Jupiter.draw(g);
        Saturn.draw(g);
        Earth.draw(g);
        moon.draw(g);
        Mars.draw(g);

        for (Planet smallPlant : planets){
            smallPlant.draw(g);
        }
        Mercury.draw(g);
        Venus.draw(g);

        Uranus.draw(g);
        Neptune.draw(g);
//        Pluto.draw(g);
        Halley.draw(g);
        shower.draw(g);


    }

    public static void main(String[] args) {
        new SolarSystemClient();
    }

}
